package com.haiyou.common.spring.log.warning.appender;


import org.springframework.util.ObjectUtils;

import com.haiyou.common.event.asyc.AsyncEventBuses;
import com.haiyou.common.spring.log.warning.event.ExceptionLogEvent;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.ConsoleAppender;

/**
 * 
 * @Description: 异常日志处理器
 * @author xingyuan
 * @date 2022年9月7日 下午3:24:34
 */
public class ExceptionLogAppender extends ConsoleAppender<ILoggingEvent> {

	@Override
	protected void append(ILoggingEvent event) {

		//开启开关
		if(!com.haiyou.common.system.Properties.log_warning_open()) {
			return ;
		}
		
		if (!isStarted()) {
			return;
		}

		// 非异常日志不需要处理
		if (event.getLevel() != Level.ERROR) {
			return;
		}

		IThrowableProxy iThrowableProxy = event.getThrowableProxy();

		if (ObjectUtils.isEmpty(iThrowableProxy)) {
			return;
		}
		
		//部分异常需要忽略
		if(com.haiyou.common.system.Properties
				.log_warning_ignore_class()
				.contains(iThrowableProxy.getClassName())) {
			return ;
		}
		
		//抛出事件
		AsyncEventBuses.getInstance().postEvent(ExceptionLogEvent.create(event));
	}

}
